﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Data.SqlClient;
using System.Collections.Generic;
using Library;

namespace BusinessLayer
{
   public class Admin
    {
        #region "Variables and Properties"
//Variables
        private int admin_ID;
        private string admin_Name;
        private string admin_Email;
        private string password;
        private string admin_Tel;
        private int admin_Status;
        
//Properties
        public int Admin_ID
        {
            get { return admin_ID; }
            set { admin_ID = value; }
        }
        public string Admin_Name
        {
            get { return admin_Name; }
            set { admin_Name = value; }
        }
        public string Admin_Email
        {
            get { return admin_Email; }
            set { admin_Email = value; }
        }
        public string Password
        {
            get { return password; }
            set { password = value; }
        } 
        public string Admin_Tel
        {
            get { return admin_Tel; }
            set { admin_Tel = value; }
        }
        public int Admin_Status
        {
            get { return admin_Status; }
            set { admin_Status = value; }
        }
#endregion

#region "Initial Methods"
        public Admin() { }
        public Admin(SqlDataReader sdr)
        {
            if (sdr != null && !sdr.IsClosed)
                admin_ID = sdr.GetInt32(0);
            if (!sdr.IsDBNull(1))
                admin_Name = sdr.GetString(1);
            if (!sdr.IsDBNull(2))
                admin_Email = sdr.GetString(2);
            if (!sdr.IsDBNull(3))
                password = sdr.GetString(3);
            if (!sdr.IsDBNull(4))
                admin_Tel = sdr.GetString(4);
            if (!sdr.IsDBNull(5))
                admin_Status = sdr.GetInt32(5);
        }
#endregion

#region "Business Methods"
//Get all Administrator
        public static List<Admin> GetAll()
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Admin_SHOWALL";
            List<Admin> lstAdmin = new List<Admin>();
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            while (sdr.Read())
            {
                lstAdmin.Add(new Admin(sdr));
            }
            return lstAdmin;
        }
//Get a Administrator
        public static Admin GetOne(int adminID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@Admin_ID", adminID);
            string cmd = "tbl_Admin_SHOWONE";
            Admin admin = new Admin();
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if (sdr.Read())
            {
                admin.Admin_ID = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    admin.Admin_Name = sdr.GetString(1);
                if (!sdr.IsDBNull(2))
                    admin.Admin_Email = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    admin.Password = sdr.GetString(3);
                if (!sdr.IsDBNull(4))
                    admin.Admin_Tel = sdr.GetString(4);
                if (!sdr.IsDBNull(5))
                    admin.Admin_Status = sdr.GetInt32(5);
            }
            return admin;
        }
//Insert a Administrator
        public static void Insert(Admin admin)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@Admin_Name", admin.Admin_Name);
            dbh.AddParameter("@Admin_Email", admin.Admin_Email);
            dbh.AddParameter("@Password", admin.Password);
            dbh.AddParameter("@Admin_Tel", admin.Admin_Tel);
            string cmd = "tbl_Admin_INSERT";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
//Update a Administrator
        public static void Update(Admin admin)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@Admin_ID", admin.Admin_ID);
            dbh.AddParameter("@Admin_Name", admin.Admin_Name);
            dbh.AddParameter("@Admin_Email", admin.Admin_Email);
            dbh.AddParameter("@Admin_Tel", admin.Admin_Tel);
            string cmd = "tbl_Admin_UPDATE";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
//Delete a Administrator
        public static void Delete(int adminID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@Admin_ID", adminID);
            string cmd = "tbl_Admin_DELETE";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
//Login
        public static Admin Login(string adminEmail, string password)
        {
            DBHelp dbh = new DBHelp();
            Admin admin = new Admin();
            dbh.AddParameter("@Admin_Email", adminEmail);
            dbh.AddParameter("@Password", password);
            string cmd = "tbl_Admin_LOGIN";
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if (sdr.Read())
            {
                admin.Admin_ID = sdr.GetInt32(0);
                if (sdr.IsDBNull(1))
                    admin.Admin_Name = sdr.GetString(1);
                if (sdr.IsDBNull(2))
                    admin.Admin_Email = sdr.GetString(2);
                if (sdr.IsDBNull(3))
                    admin.Password = sdr.GetString(3);
                if (sdr.IsDBNull(4))
                    admin.Admin_Tel = sdr.GetString(4);
                if (sdr.IsDBNull(5))
                    admin.Admin_Status = sdr.GetInt32(5);
            }
            return admin;
        }
#endregion
    }
}
